package com.deeper.customview.ui.c2.exercise.c02e01;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 请为 3.5 节中的手表添加一个背景作为表盘， 让手表看起来更真实
 */
public class WatchView extends View {
    private Paint paint;
    private Calendar calendar;
    private String[] hourPoint;


    public WatchView(Context context) {
        super(context);
    }


    public WatchView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.GRAY);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(1);

        calendar = Calendar.getInstance();

        hourPoint = new String[]{"Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", "Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ"};
    }


    public WatchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //获取组件宽高
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        //计算圆盘直径
        int len = Math.min(width, height);
        //绘制表盘
        drawPlate(canvas, len);
        //绘制背景
        drawBackground(canvas, len);
        //绘制指针
        drawPoints(canvas, len);
    }


    public void run() {
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                postInvalidate();
            }
        }, 0, 1000);
    }


    private void drawPlate(Canvas canvas, int len) {
        canvas.save();
        //画圆
        int r = len / 2;
        canvas.scale(0.7f, 0.7f, r, r);

        canvas.drawCircle(r, r, r, paint);
        //画刻度
        for (int i = 0; i < 60; i++) {
            if (i % 5 == 0) {
                //长刻度
                paint.setColor(Color.RED);
                paint.setStrokeWidth(4);
                canvas.drawLine(r + 9 * r / 10, r, len, r, paint);
            } else {
                //短刻度
                paint.setColor(Color.GRAY);
                paint.setStrokeWidth(1);
                canvas.drawLine(r + 14 * r / 15, r, len, r, paint);
            }
            canvas.rotate(6, r, r);
        }
        canvas.restore();
    }


    private void drawBackground(Canvas canvas, int len) {
        //画数字
        canvas.save();
        int r = len / 2;
        canvas.scale(0.7f, 0.7f, r, r);

        canvas.rotate(-60, r, r);
        for (int i = 0; i < 12; i++) {
            canvas.save();
            paint.setTextSize(40);
            paint.setStyle(Paint.Style.FILL);
            canvas.rotate(90, len + 20, r);
            canvas.drawText(hourPoint[i], len, r, paint);
            canvas.restore();

            canvas.rotate(30, r, r);
        }
        canvas.restore();
    }


    private void drawPoints(Canvas canvas, int len) {
        //先获取系统时间
        calendar.setTimeInMillis(System.currentTimeMillis());
        //获取时分秒
        int hours = calendar.get(Calendar.HOUR);
        int minutes = calendar.get(Calendar.MINUTE);
        int seconds = calendar.get(Calendar.SECOND);

        //画时针
        //1 计算角度
        int degree = 360 / 12 * hours;
        double radians = Math.toRadians(degree);
        //2 计算起点和终点
        int r = len / 2; //半径
        int startX = r;
        int startY = r;
        int endX = (int) (startX + 0.5 * r * Math.cos(radians));
        int endY = (int) (startY + 0.5 * r * Math.sin(radians));
        canvas.save();
        canvas.scale(0.7f, 0.7f, r, r);

        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(3);
        //因为 0度从3点处开始，时间从12点处开始，所以需要将画布旋转90度
        canvas.rotate(-90, r, r);
        canvas.drawLine(startX, startY, endX, endY, paint);
        canvas.restore();

        //画分针
        //1 计算角度
        degree = 360 / 60 * minutes;
        radians = Math.toRadians(degree);
        //计算起点和终点
        endX = (int) (startX + 0.6 * r * Math.cos(radians));
        endY = (int) (startY + 0.6 * r * Math.sin(radians));
        canvas.save();
        canvas.scale(0.7f, 0.7f, r, r);

        paint.setStrokeWidth(2);
        //因为 0度从3点处开始，时间从12点处开始，所以需要将画布旋转90度
        canvas.rotate(-90, r, r);
        canvas.drawLine(startX, startY, endX, endY, paint);
        canvas.restore();

        //画秒针
        //1 计算角度
        degree = 360 / 60 * seconds;
        radians = Math.toRadians(degree);
        //计算起点和终点
        endX = (int) (startX + 0.8 * r * Math.cos(radians));
        endY = (int) (startY + 0.8 * r * Math.sin(radians));
        canvas.save();
        canvas.scale(0.7f, 0.7f, r, r);

        paint.setStrokeWidth(1);
        //因为 0度从3点处开始，时间从12点处开始，所以需要将画布旋转90度
        canvas.rotate(-90, r, r);
        canvas.drawLine(startX, startY, endX, endY, paint);
        canvas.restore();
    }
}
